<template>
  <div>
    <van-row>
      <van-nav-bar fixed=true placeholder=true title="鲶鱼"></van-nav-bar>
      <van-swipe :autoplay="1000" class="my-swipe" :show-indicators="false">
        <van-swipe-item v-for="(image, index) in images" :key="index">
          <van-image lazy-load v-bind:src="image.timages[0].url" style="height: inherit;width: inherit"
            @click="goToDetail(image.timages[0].productId)" fit="scale-down" />
        </van-swipe-item>
      </van-swipe>
      <van-grid :column-num="4">
        <van-grid-item v-for="(obj, index) in objs" :key="index" style="width:25%;height:140px;">
          <van-image v-bind:src="obj.timages[0].url" @click="goToDetail(obj.timages[0].productId)" width="5rem"
            height="5rem" fit="cover">
          </van-image>
          <div class="miaoshu"> {{  obj.product.title  }}</div>
        </van-grid-item>
      </van-grid>
    </van-row>
    <div class="search">
      <van-search v-model="value" show-action label="search" placeholder="请输入搜索关键词" @search="onSearch">
      </van-search>
      <van-icon @click="toPhone" class="searchicon" name="bars">
        <span @click="toPhone" class="searchicon2">分类</span>
      </van-icon>
    </div>
    <van-card v-for="(detailItem, index) in tableData" 
    :key="index" 
    :price="detailItem.product.price" 
    :title="detailItem.product.title" 
    :desc="detailItem.product.description"
    :thumb="detailItem.timages[0].url" 
    @click="toDetails(detailItem.timages[0].productId)">
    </van-card>
    <van-button class="buttonmore" type="primary" @click="expand">查看更多</van-button>
    <div style="height:100px"></div>
  </div>
</template>
<script>
let number = 0
export default {
  data() {
    return {
      value: '',
      tableData: [],
      totalData: [],
      active: '0',
      images: [],
      objs: [],
    };
  },
  created() {
    this.axios({
      method: "GET",
      url: "/aapi/toitems"
    }).then(response => {
      this.images = response.data;
    })
    this.axios({
      method: "GET",
      url: "/aapi/toitems"
    }).then(response => {
      this.objs = response.data;
    })
    number = 4
    this.value = this.$store.state.sousuoinput
    this.axios({
      method: 'GET', url: "/aapi/select", params: {
        value: this.value
      }
    }).then((response) => {
      this.totalData = response.data
      this.tableData = response.data.slice(0, number)
    })
  },
  methods: {
    goToDetail(id) {
      this.$store.commit('changeproductid', id)
      this.$router.push({
        path: '/goodsdetail'
      })
    },
    toPhone() {
      this.$router.push({
        path: '/laptop',
      })
    },
    expand() {
      number = number + 4
      this.tableData = this.totalData.slice(0, number)
    },
    toDetails(id) {
      this.$store.commit("changeproductid", id)
      this.$router.push({
        path: '/goodsdetail',
      })
    },
    onSearch() {
      number = 4
      this.axios({
        method: 'GET', url: "/aapi/select", params: {
          value: this.value
        }
      }).then((response) => {
        this.totalData = response.data
        this.tableData = response.data.slice(0, number)
      })
    },
  }
};

</script>
<style scoped>
.my-swipe .van-swipe-item {

  width: 100%;
  height: 150px;
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
}


.notice-swipe {
  height: 40px;
  line-height: 40px;
}

.miaoshu {
  width: inherit;
  height: 20px;
  line-height: 20px;

}

.buttonmore{
  margin-top: 20px;
}
.search {
  display: flex;
}

.searchicon {
  position: relative;
  top: 10px;
  left: 20px;
}

.searchicon2 {
  position: absolute;
  top: 20px;
  right: 1%;
  font-size: 12px;
  width: 25px;
}
</style>